if SERVER then
	AddCSLuaFile( "shared.lua" )
end

if CLIENT then
	SWEP.Slot				= 3
	SWEP.SlotPos			= 1
	SWEP.DrawCrosshair      = true
	SWEP.DrawAmmo = true
end

SWEP.PrintName			= "Breaching Charge"
SWEP.Author				= "Jammie Dodger"
SWEP.Instructions		= "Left click on a door to breach."
	
SWEP.Spawnable			= false
SWEP.AdminSpawnable		= true

SWEP.ViewModel				= "models/weapons/v_c4.mdl"
SWEP.WorldModel				= "models/weapons/w_c4.mdl"

SWEP.Primary.ClipSize		= 2
SWEP.Primary.DefaultClip	= 2
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= "none"
SWEP.Primary.Delay			= 9

SWEP.Secondary.ClipSize		= -1  
SWEP.Secondary.DefaultClip	= -1  
SWEP.Secondary.Automatic	= true
SWEP.Secondary.Ammo			= "none" 

function SWEP:Initialize()
	self:SetWeaponHoldType( "normal" )
end

function SWEP:PrimaryAttack( )
	
	local tr = self.Owner:GetEyeTrace( )
	
	if ( tr.Entity:GetClass() == "prop_door_rotating" && tr.Entity:GetPos( ):Distance( self.Owner:GetPos( ) ) < 100 ) and !tr.Entity.HasExplosive then
	
		if !tr.Entity.Owners then
			chat.AddText(self.Owner, Color(100,149,237,255), "This door currently has no owner.")
			return
		end
		
		local Person = FindPlayerUnique(tr.Entity.Owners)
		
		if Person.GuiltNumber <= 0  then
			chat.AddText(self.Owner, Color(100,149,237,255), "The owner is not guilty!")
			return
		end			
		
		tr.Entity:EmitSound( "npc/turret_floor/deploy.wav" )
		
		if ( SERVER ) then
		
			local ent = ents.Create( "prop_dynamic_override" )
				ent:SetModel( "models/weapons/w_c4_planted.mdl" ) 
				ent:SetPos( tr.HitPos )
				ent:SetAngles(tr.HitNormal:Angle() + Angle(-90, 0, 180))
				ent:SetMoveType( MOVETYPE_NONE )
				ent:SetSolid( SOLID_VPHYSICS )
				ent:SetParent( tr.Entity )
				ent:SetCollisionGroup(COLLISION_GROUP_NONE)
				ent:Spawn( ) 
				
				tr.Entity.HasExplosive = true
		
			timer.Simple( 1, function( ) ent:EmitSound( "hl1/fvox/beep.wav" ) end )
			timer.Simple( 2, function( ) ent:EmitSound( "hl1/fvox/beep.wav" ) end )
			timer.Simple( 3, function( ) ent:EmitSound( "hl1/fvox/beep.wav" ) end )
			timer.Simple( 4, function( ) ent:EmitSound( "hl1/fvox/beep.wav" ) end )
			timer.Simple( 5, function( ) ent:EmitSound( "hl1/fvox/beep.wav" ) end )
			timer.Simple( 6, function( ) ent:EmitSound( "hl1/fvox/beep.wav" ) end )
			timer.Simple( 7, function( ) ent:EmitSound( "hl1/fvox/beep.wav" ) end )
			timer.Simple( 8, function( ) 
			
			tr.Entity:SetNotSolid(true)
			tr.Entity:SetNoDraw(true)
			local Collision = tr.Entity:GetCollisionGroup()
			tr.Entity:SetCollisionGroup(COLLISION_GROUP_WORLD)
			
			local door = ents.Create( "prop_physics" )
				door:SetModel( tr.Entity:GetModel( ) )
				door:SetPos( tr.Entity:GetPos( ) )
				door:SetAngles( tr.Entity:GetAngles( ) )
				door:SetSkin( tr.Entity:GetSkin( ) )
				door:Spawn( ) 
				door:GetPhysicsObject( ):SetVelocity( self.Owner:GetPos( ) + self.Owner:GetForward( ) * 10373 )
			
			local explode = ents.Create( "env_explosion" )
				explode:SetKeyValue( "spawnflags", 128 )
				explode:SetPos( tr.Entity:GetPos( ) )
				explode:Spawn( )
				explode:Fire( "explode", "", 0 )
				ent:Remove( )
				
				timer.Simple( 60, function( ) door:Remove() end)
				timer.Simple( 80, function( ) tr.Entity:SetNotSolid(false) tr.Entity.HasExplosive = false tr.Entity:SetNoDraw(false) tr.Entity:SetCollisionGroup(Collision) end )
		
			end )
			
		end
		
	end
	
	self:TakePrimaryAmmo( 1 )
	
end
